//
//  KZProductDetailDescriptionCell.swift
//  BoyeStore
//
//  Created by 朱凯 on 2018/6/26.
//  Copyright © 2018年 itboye. All rights reserved.
//

import UIKit

import WebKit


class KZProductDetailDescriptionCell: UITableViewCell {
    
    let titleLabel = UILabel()
    let webView = WKWebView()
    
    var observer: NSKeyValueObservation!
    
//    deinit {
//
//        removeObserver()
//    }
//
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

// MARK: ------ 创建视图
extension KZProductDetailDescriptionCell {
    
    func setupUI() {
        
        titleLabel.textColor = UIColor.gray
        titleLabel.numberOfLines = 1
        titleLabel.textAlignment = .left
        titleLabel.text = "商品简介"
        titleLabel.font = UIFont.systemFont(ofSize: 14)
        contentView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (m) in
            m.left.equalTo(10)
            m.right.equalTo(-10)
            m.top.equalTo(0)
            m.height.equalTo(40)
        }


        let line = UIView()
        contentView.addSubview(line)
        line.backgroundColor = kBgColor
        line.snp.makeConstraints { (m) in
            m.left.right.equalTo(0)
            m.bottom.equalTo(titleLabel)
            m.height.equalTo(1)
        }
        
        
        contentView.addSubview(webView)
        
        webView.snp.makeConstraints { (m) in
            m.left.right.equalTo(0)
            m.top.equalTo(line.snp.bottom)
            m.height.equalTo(1000)
            
            m.bottom.equalTo(0)
        }
        webView.load(URLRequest(url: URL(string: "http://www.baidu.com")!))
    }
    
    
}

// MARK: ------ 观察者
extension KZProductDetailDescriptionCell {
    
    
    func addObserver() {
        observer =  webView
            .observe(\WKWebView.scrollView.contentSize, options: [.new])
            { [weak self] (_, change) in
                self!.webView.snp.updateConstraints({ (m) in
                    m.height.equalTo(change.newValue!.height)
                })
                
        }
    }
}


